package com.jhws.business.comm.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import org.springframework.transaction.annotation.Transactional;

import com.jhws.business.comm.bo.TblAreaDimBo;
import com.jhws.common.Page;
import com.jhws.core.db.BaseDao;

@Transactional
public class TblAreaDimHibernateDAO extends BaseDao implements ITblAreaDimDAO {

	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public TblAreaDimBo getById(String areaCode ,String commCode) {
		
		Query q = sessionFactory.getCurrentSession().createQuery("from TblAreaDimBo where area_code = ? and comm_Code = ?");
		q.setParameter(0, areaCode);
		q.setParameter(1, commCode);
		List<TblAreaDimBo> list = q.list();
		if (list.size() != 0)
			return (TblAreaDimBo) list.get(0);
		else
			return null;
	}
	
	public TblAreaDimBo getAreaById(String areaCode ,String commCode) {
		return this.getById(areaCode, commCode);
	}

	public Page findTblAreaDim(int start, int num, String whereClause)
			throws Exception {
		try {
			StringBuffer buf = new StringBuffer();
			buf.append("from TblAreaDimBo ");
			buf.append(whereClause);
			buf.append("order by CAST(area_name AS DECIMAL) ");
			  
			Query query = sessionFactory.getCurrentSession().createQuery(buf.toString());
			query.setFirstResult(start);
			query.setMaxResults(num);
			List list = query.list();

			Page page = new Page(start, num, list);
			String hql = "select count(*) from TblAreaDimBo "+ (whereClause == null || "".equals(whereClause.trim()) ? "" : whereClause.trim());
			query = sessionFactory.getCurrentSession().createQuery(hql);
			page.setTotalNumber(((Number)query.uniqueResult()).intValue());
			return page;
		} catch (Exception e) {
			System.out.println("=========e:"+e.getMessage());
			return new Page();
		}
	}

	public TblAreaDimBo save(TblAreaDimBo bo) {
		sessionFactory.getCurrentSession().saveOrUpdate(bo);
		return bo;
	}

	public List<TblAreaDimBo> findTblAreaDimByWhere(String whereClause) throws Exception {
		try {
			StringBuffer buf = new StringBuffer("from TblAreaDimBo ");
			if(whereClause != null || !"".equals(whereClause))
				buf.append(whereClause);
			buf.append(" order by CAST(area_name AS DECIMAL) ");
			Query query = sessionFactory.getCurrentSession().createQuery(buf.toString());
			
			@SuppressWarnings("unchecked")
			List<TblAreaDimBo> list = query.list();
			return list;
		} catch (Exception e) {
			return new ArrayList<TblAreaDimBo>();
		}
	}

	public void deleteTblAreaDim(String areaCode ,String commCode) {
		if(areaCode != null){
			TblAreaDimBo bo = getAreaById(areaCode,commCode);
			if (null != bo)
			{
				sessionFactory.getCurrentSession().delete(bo);
			}
		} else
			try {
				this.deleteByColumns("TblAreaDimBo", "comm_code", commCode);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}


	public TblAreaDimBo getTblArea(TblAreaDimBo bo) {
		Query q = sessionFactory.getCurrentSession().createQuery("from TblAreaDimBo where Area_Name = ?");
		q.setParameter(0, new Object[]{bo.getAreaName()});
		List list = q.list();
		
		if(list.size() != 0)
			return (TblAreaDimBo) list.get(0);
		else 
			return null;
	}

}	
